package com.lgcns.user.spring;

import java.security.Principal;

import com.lgcns.commons.WebContents;
import com.lgcns.user.User;
import com.lgcns.user.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/users")
public class UsersController
{
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/signUp", method=RequestMethod.GET)
	public String signUp()
	{
		return "users/signUp";
	}
	
	@RequestMapping(value="/signUp", method=RequestMethod.POST)
	public String signUp(User user) throws Exception
	{
		String authority = "ROLE_USER";
		
		int check = userService.insert(user);
		
		if (check == 1)
		{
			userService.insertAuthority(user.getEmail(), authority);
		}
		
		return "users/welcome";
	}
	
	@RequestMapping(value="/login", method=RequestMethod.GET)
	public String login()
	{
		return "users/login";
	}
	
	/*@RequestMapping(value="/login", method=RequestMethod.POST)
	public String login(User user, HttpSession session) throws Exception
	{
		user = userService.login(user);
		if (user != null)
		{
			session.setAttribute("user", user);
			return "users/changePasswd";
		} else
		{
			return "users/login";
		}
	}*/
	
	@RequestMapping(value="/editAccount", method=RequestMethod.GET)
	public String editAccount(Principal principal, Model model)
	{
		User user = userService.getUserByEmail(principal.getName());
		model.addAttribute(WebContents.USER_KEY, user);
		
		return "users/editAccount";
	}
	
	@RequestMapping(value="/editAccount", method=RequestMethod.POST)
	public String editAccount(User user, Principal principal)
	{
		
		User loginUser = userService.login(user);

		if (!(user.getPasswd().equals(loginUser.getPasswd())))
		{
			throw new AccessDeniedException("비빌번호가 틀립니다.");
		}

		if (user.getName() == null || user.getName().equals(""))
		{
			user.setName(loginUser.getName());
		}
		
		if (user.getMobile() == null || user.getMobile().equals(""))
		{
			user.setMobile(loginUser.getMobile());
		}

		user.setEmail(principal.getName());
		
		userService.modify(user);
		
		return "redirect:/users/changePasswd";
		
	}
	
	@RequestMapping(value="/changePasswd", method=RequestMethod.GET)
	public String changePasswd(Principal principal, Model model)
	{
		User user = userService.getUserByEmail(principal.getName());
		
		model.addAttribute(WebContents.USER_KEY, user);
		
		return "users/changePasswd";
	}
	
	@RequestMapping(value="/changePasswd", method=RequestMethod.POST)
	public String changePasswd(String passwd,Principal principal)
	{

		User user = new User();
		user.setEmail(principal.getName());
		user.setPasswd(passwd);
		
		userService.changePasswd(user);
		
		return "redirect:/users/changePasswd";
	}
	
	@RequestMapping(value="/bye", method=RequestMethod.GET)
	public String bye(Principal principal)
	{
		/*
		 * 1.권한테이블에서 레코드 삭제
		 * 2.회원테이블 레코드 삭제
		 * 3. 로그아웃
		 */
		return "users/bye";//bye.jsp 는 새로 만들어야 한다.
	}
	
}